#!/usr/bin/env python

# 将trf的输出文件 转化为gff3格式

import argparse
import sys
parser = argparse.ArgumentParser(
    description='将trf的输出文件转化为gff3格式，由大天才于2021年6月11日创建于浙江农业大学')

parser.add_argument('-i',
                help='必须给定，输入的trf的输出文件（就是stdout的文件）')
parser.add_argument('-p',
                help='必须给定，前缀')

parser.add_argument('-o',
                help='必须给定，输出文件')



args = parser.parse_args()

if not args.i or not args.p or not args.o:
    parser.print_help()
    sys.exit()

infile = args.i
prefix = args.p

outfile = args.o



seq_name = ''


n=1



outfila = open(outfile,'w')

outfila.write('##gff-version 3.2.1\n')



with open(infile) as fila:
    for i in fila:
        
        if i[0] == '@':
            seq_name = i[1:].strip()
        else:
            k = i.split(' ')
            a = 0
            try:
                int(k[0])
                a = 1
            except:
                pass
            if a ==1:
                Id = prefix+'_'+str(n)
                start = k[0]
                end = k[1]
                PeriodSize = k[2]
                CopyNumber = k[3]
                ConsensusSize = k[4]
                PercentMatches = k[5]
                PercentIndels = k[6]
                Score=k[7]
                Entropy = k[12]
                n+=1
                outfila.write(seq_name + '\t'
                    +'TRF'+'\t'+'tandem_repeat'+'\t'
                    + start+'\t' + end + '\t'+Score+'\t'+'.'+'\t'+'.'+'\t'+
                    'ID='+Id+';'+'PeriodSize='+PeriodSize+';'+
                    'PeriodSize='+PeriodSize+';'+
                    'CopyNumber='+CopyNumber+';'+
                    'ConsensusSize='+ConsensusSize+';'+
                    'PercentMatches='+PercentMatches+';'+
                    'PercentIndels='+PercentIndels+';'+
                    'Score='+Score+';'+
                    'Entropy='+Entropy+';'+'\n'

                    )
                
